﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;

namespace Bus_proj.website
{
    public partial class reportfinal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }                
            }
        }

        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years < 2300)
                {
                    years = years + 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (years2 + "-" + mount2 + "-" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "-" + mount3 + "-" + day3);
        }
        public string ConvestToDate_check2(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (years2 + "-" + mount2 + "-" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "-" + mount3 + "-" + day3);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string start = ConvestToDate_check2(TextBox1.Text);
            string end = ConvestToDate_check2(TextBox2.Text);
            start = start + " " + "00:00:00";
            end = end + " " + "23:59:59";
            if (DateTime.Parse(start)<=DateTime.Parse(end))
            {
                Session["start_date"] = start;
                Session["end_date"] = end;
                DateTime date_start = DateTime.Parse(ConvestToDate_check(TextBox1.Text));
                DateTime date_end = DateTime.Parse(ConvestToDate_check(TextBox2.Text));
                string start2 = String.Format("{0:'วัน'dddd'ที่' d MMMM gg yyyy}", date_start);
                string end2 = String.Format("{0:'วัน'dddd'ที่' d MMMM gg yyyy}", date_end);
                //ReportViewer1.LocalReport.DataSources.Clear();
                ReportParameter[] param = new ReportParameter[2];
                param[0] = new ReportParameter("data01", start2);
                param[1] = new ReportParameter("data02", end2);
                this.ReportViewer1.LocalReport.SetParameters(param);
                ReportViewer1.LocalReport.Refresh();
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('กรุณากรอกวันที่ให้ถูกต้อง');", true);
            }
            
        }
    }
}